//
//  BEPickerView
//  demo
//
//  Created by KKING on 16/7/28.
//  Copyright © 2016年 Bullseye Energy pte ltd. All rights reserved.
//

import UIKit

/**-------------------------site type-------------------------*/
class BESiteIPView: UIView,UIPickerViewDataSource,UIPickerViewDelegate {
    
    var siteData: [BESiteModel] = [BESiteModel]()
    var currentRowNumber: ((_ rowNumber: Int) -> ())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        let pickerView = UIPickerView()
        pickerView.dataSource = self
        pickerView.delegate = self
        pickerView.frame = frame
        addSubview(pickerView)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @available(iOS 2.0, *)
    public func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return siteData.count + 1
    }
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if row == 0{
            return "choose"
        }
        return siteData[row - 1].name
    }
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 50
    }
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        currentRowNumber!(row - 1)
    }
}



/**-------------------------job type-------------------------*/
class BEJobTypeIPView: UIView,UIPickerViewDataSource,UIPickerViewDelegate {
    
    var jobTypeData: [BEJobTypeModel] = [BEJobTypeModel]()
    
    var currentRowNumber: ((_ rowNumber: Int) -> ())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        let pickerView = UIPickerView()
        pickerView.dataSource = self
        pickerView.delegate = self
        pickerView.frame = frame
        addSubview(pickerView)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @available(iOS 2.0, *)
    public func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return jobTypeData.count + 1
    }
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if row == 0{
            return "choose"
        }
        return jobTypeData[row - 1].name
    }
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 50
    }
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        currentRowNumber!(row - 1)
    }
}


/**-------------------------type-------------------------*/
class BEEquipTypeIPView: UIView,UIPickerViewDataSource,UIPickerViewDelegate {
    
    var equipTypeData: [BEEquipTypeModel] = [BEEquipTypeModel]()
    
    var currentRowNumber: ((_ rowNumber: Int) -> ())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        let pickerView = UIPickerView()
        pickerView.dataSource = self
        pickerView.delegate = self
        pickerView.frame = frame
        addSubview(pickerView)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    @available(iOS 2.0, *)
    public func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return equipTypeData.count + 1
    }
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if row == 0{
            return "choose"
        }
        return equipTypeData[row - 1].name
    }
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 50
    }
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        currentRowNumber!(row - 1)
    }
}

/**-------------------------brand-------------------------*/
class BEEquipBrandIPView: UIView,UIPickerViewDataSource,UIPickerViewDelegate {
    
    var equipBrandData: [BEEquipBrandModel] = [BEEquipBrandModel]()
    
    var currentRowNumber: ((_ rowNumber: Int) -> ())?
    var equmdlData: ((_ equmdlData: [BEEqmdlModel]) -> ())?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        let pickerView = UIPickerView()
        pickerView.dataSource = self
        pickerView.delegate = self
        pickerView.frame = frame
        addSubview(pickerView)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @available(iOS 2.0, *)
    public func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return equipBrandData.count + 1
    }
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if row == 0{
            return "choose"
        }
        return equipBrandData[row - 1].name
    }
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 50
    }
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        currentRowNumber!(row - 1)
        if row == 0{
            return
        }
        //在此处发送请求 获取当前设备.
        BENetworkTools.shareNetworkTool.getEquipModelBy(brandId: equipBrandData[row - 1].id!) { (success, result, error) in
            guard let resultjson = result else {
                //                JFProgressHUD.showWithStatus(status: "您的网络不给力")
                return
            }
            if resultjson["success"] == true {
                //                print(resultjson)
                let data = resultjson["result"].arrayObject as! [[String : AnyObject]]
                var equipmdlData: [BEEqmdlModel] = [BEEqmdlModel]()
                for item in data{
                    let eqmdl = BEEqmdlModel(dict: item)
                    //此处把数据添加到数组并设置给inputView
                    equipmdlData.append(eqmdl)
                }
                //此处回到主线程把数据绑定给inputView
                DispatchQueue.main.async(execute: {
                    //此处把已经转完模型并添加到数组中的数据给下一个inputView
                    self.equmdlData!(equipmdlData)
                })
                //                JFProgressHUD.showSuccessWithStatus(status: "confirm package success!")
            } else {
                //                JFProgressHUD.showInfoWithStatus(status: resultjson["message"].stringValue)
            }
        }
    }
}

/**-------------------------model-------------------------*/
class BEEquipmdlIPView: UIView,UIPickerViewDataSource,UIPickerViewDelegate {
    
    var equipMdlData: [BEEqmdlModel] = [BEEqmdlModel]()
    
    var currentRowNumber: ((_ rowNumber: Int) -> ())?
    var equEquipData: ((_ equEquipData: [BEEquipEquipModel]) -> ())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        let pickerView = UIPickerView()
        pickerView.dataSource = self
        pickerView.delegate = self
        pickerView.frame = frame
        addSubview(pickerView)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @available(iOS 2.0, *)
    public func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return equipMdlData.count + 1
    }
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if row == 0{
            return "choose"
        }
        return equipMdlData[row - 1].name
    }
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 50
    }
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        currentRowNumber!(row - 1)
        if row == 0{
            return
        }
        //获取设备
        let moreIdParameters = [
            //            "typeId": equtypeId ?? "",
            "modelId": equipMdlData[row - 1].id ?? "",
            //            "brandId": equBrandId ?? ""
        ]
        BENetworkTools.shareNetworkTool.getEquipBy(companyId: (BEAccountModel.shareAccount()?.companyId)!, parameters: moreIdParameters as [String : AnyObject]) { (success, result, error) in
            guard let resultjson = result else {
                //                finished(false, "Network fault!")
                return
            }
//            print(resultjson)
            if resultjson["success"] == true {
                let data = resultjson["result"].arrayObject as! [[String : AnyObject]]
                var equipEquipData: [BEEquipEquipModel] = [BEEquipEquipModel]()
                for item in data{
                    let eqEquip = BEEquipEquipModel(dict: item)
                    //此处把数据添加到数组并设置给inputView
                    equipEquipData.append(eqEquip)
                }
                //此处回到主线程把数据绑定给inputView
                DispatchQueue.main.async(execute: {
                    //此处把已经转完模型并添加到数组中的数据给下一个inputView
                    self.equEquipData!(equipEquipData)
                })
                //                finished(true, "订单创建成功")
            } else {
                //                finished(false, resultjson["message"].stringValue)
            }
        }
        
    }
}


/**-------------------------equip-------------------------*/
class BEEquEquipIPView: UIView,UIPickerViewDataSource,UIPickerViewDelegate {
    
    var equipEquipData: [BEEquipEquipModel] = [BEEquipEquipModel]()
    var currentRowNumber: ((_ rowNumber: Int) -> ())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        let pickerView = UIPickerView()
        pickerView.dataSource = self
        pickerView.delegate = self
        pickerView.frame = frame
        addSubview(pickerView)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @available(iOS 2.0, *)
    public func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return equipEquipData.count + 1
    }
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if row == 0{
            return "choose"
        }
        return equipEquipData[row - 1].name
    }
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 50
    }
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        currentRowNumber!(row - 1)
    }
}

